<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="楼栋名称" prop="buildingName">
        <el-input
          v-model="queryParams.buildingName"
          placeholder="请输入楼栋名称"
          :clearable="true"
          @keyup.enter="getPage"
        />
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['bed:building:query']" @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
        <el-button v-hasPermi="['bed:building:create']" @click="handleClickCreate">
          <Icon icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      :adaptive="true"
      :page-data="queryData"
      save-key="BuildingInfo"
      height-per="90"
      @page-change="getPage"
      @row-dblclick="handleTableDblclick"
    >
      <template #menu="{ row }">
        <context-menu-item
          label="修改"
          @click="handleMenuUpdate(row)"
          v-hasPermi="['bed:building:update']"
        />
        <context-menu-item
          label="删除"
          @click="handleMenuDeleted(row)"
          v-hasPermi="['bed:building:deleted']"
        />
      </template>
    </Table>
  </ContentWrap>
  <BuildingInfoEdit ref="editFormRef" @success="getPage" />
</template>
<script setup name="BuildingInfo" lang="ts">
import * as BuildingInfoApi from '@/api/bed/buildingInfo'
import BuildingInfoEdit from './BuildingInfoEdit.vue'
const message = useMessage() // 消息弹窗
const editFormRef = ref()
const queryFormRef = ref()
const queryParams = ref({
  buildingName: undefined
})
const queryData = ref()
const columns = [
  {
    label: '楼栋名称',
    prop: 'buildingName'
  },
  {
    label: '楼层数',
    prop: 'floorNumber'
  },
  {
    label: '房间数量',
    prop: 'roomNumber'
  },
  {
    label: '床位数量',
    prop: 'bedNumber'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]
/** 初始化 */
onMounted(() => {
  getPage()
})

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 新增按钮操作 */
const handleClickCreate = () => {
  editFormRef.value.open('create', 0)
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await BuildingInfoApi.query(queryParams.value)
}

/** 处理右击事件 修改 */
const handleMenuUpdate = (row) => {
  editFormRef.value.open('update', row.id)
}

/** 处理tables双击修改事件 */
const handleTableDblclick = (row) => {
  editFormRef.value.open('update', row.id)
}

/** 处理右击事件 删除 */
const handleMenuDeleted = async (row) => {
  await message.confirm('确定要删除此楼栋吗？')
  await BuildingInfoApi.deleted(row.id)
  getPage()
}
</script>
